import NProgress from "nprogress"
import "nprogress/nprogress.css"
import Vue from "vue"
import Router from "vue-router"
import { ManagementCenter } from "./managementCenter/mIndex"
let routes = [
  {
    path: "/",
    redirect: {
      name: "userLogin",
    },
  },
  {
    path: "/userLogin",
    name: "userLogin",
    component: (solve) => require(["@/views/userLogin"], solve),
  },
  // 单独的三个页面
  {
    path: "/MyUploadSingle1",
    name: "MyUploadSingle1",
    component: (solve) =>
      require(["@/views/managementCenter/personCenter/myUpload"], solve),
  },
  {
    path: "/MyDownloadSingle1",
    name: "MyDownloadSingle1",
    component: (solve) =>
      require(["@/views/managementCenter/personCenter/myDownload"], solve),
  },
  {
    path: "/MyInfoSingle1",
    name: "MyInfoSingle1",
    component: (solve) =>
      require(["@/views/managementCenter/personCenter/myInfo"], solve),
  },
  {
    path: "/MyCollect1",
    name: "MyCollect1",
    component: (solve) =>
      require(["@/views/managementCenter/personCenter/myCollect"], solve),
  },
  {
    path: "/PersonCenter",
    name: "PersonCenter",
    component: (solve) =>
      require(["@/views/managementCenter/personCenter/personCenter"], solve),
  },
  {
    path: "/notFound",
    name: "notFound",
    meta: {
      title: "Page not found",
      isLogin: false,
    },
    component: () => import("@/views/notFound.vue"),
  },
  // 未配置的路由跳到404页面
  {
    path: "*",
    redirect: "/notFound",
  },
]
// 合并管理中心
routes = routes.concat(ManagementCenter)

const originalPush = Router.prototype.push

const originalReplace = Router.prototype.replace

Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err)
}

Vue.use(Router)
const router = new Router({
  routes: routes,
  //   base: "/zk-ui",
  //   mode: "history",
  mode: "hash",
})

NProgress.configure({
  easing: "ease",
  speed: 500,
  showSpinner: false,
  trickleSpeed: 200,
  minimum: 0.3,
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.path === "/userLogin") {
    NProgress.done()
    return next()
  }
  //获取token
  const tokenStr = localStorage.getItem("token")
  if (!tokenStr) {
    NProgress.done()
    // return next("/userLogin")
    return next("/")
  }
  next()
})

router.afterEach(() => {
  NProgress.done()
})
export default router
